<template>
    <div class="app-container">
        <el-card class="box-card">
            <el-button type="primary" size="default" @click="handleAdd">新增</el-button>
            
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="任务ID" prop="task_id"></el-table-column>
                <el-table-column label="奖励ID" prop="prize_id"></el-table-column>
                <el-table-column label="数量" prop="count"></el-table-column>
                <el-table-column label="奖励名称" prop="title"></el-table-column>
                <el-table-column label="ICON" prop="icon">
                    <template slot-scope="{row}">
                        <el-image v-if="row.icon" :src="row.icon" fit="fill" class="image" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="created_at"></el-table-column>
                <el-table-column label="操作" width="200px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleEdit(scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleDel(scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <el-dialog
            title="编辑"
            :visible.sync="dialogVisible"
            width="30%"
            @close="closeDialog('form')">
            
            <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="150px" size="normal">
                
                <el-form-item label="奖励ID" prop="prize_id">
                    <el-select v-model="formData.prize_id" placeholder="请选择任务奖励" clearable filterable>
                        <el-option v-for="item in options2"
                            :key="item.id"
                            :label="item.title"
                            :value="item.id">
                        </el-option>
                    </el-select>
                    
                </el-form-item>
                <el-form-item label="数量" prop="count">
                    <el-input type="text" :rows="2" v-model="formData.count" placeholder="请输入" 
                        :maxlength="-1" :show-word-limit="false" :autosize="{ minRows: 2, maxRows: 4 }">
                    </el-input>
                </el-form-item>
            </el-form>
            
            <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleAllocationSkill">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>
    import { balanceTaskPrizeList, balanceTaskPrizeSave, balanceTaskPrizeDelete, petPrizeList, balanceTaskList } from '@/api/petsManagement'
  	export default {
        name: 'cleaningTaskAward',
        data() {
            return {
                checked: false,
                dialogTitle: 'add',
                dialogVisible: false,
                info: {},
                loading: false,
                loading2: false,
                tableData: [],
                listQuery: {
                    task_id: undefined,
                    page: 1,
                    perPage: 10
                },
                total: 1,
                options1: [],
                options2: [],
                formData: {
                    id: undefined,
                    task_id: undefined,
                    prize_id: undefined,
                    count: undefined,
                },
                rules: {
                    task_id: [{ required: true, message: '请输入', trigger: 'blur' }],
                    prize_id: [{ required: true, message: '请输入', trigger: 'blur' }],
                    count: [{ required: true, message: '请输入', trigger: 'blur' }],
                },
                query: undefined
            }
        },
        created() {
            this.listQuery.task_id = this.$route.query.task_id
            this.formData.task_id = this.$route.query.task_id
            this.getTableData()
            this.getBalanceTaskList()
            this.getPetPrizeList()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key] == 0 ? 0 : this.listQuery[key]
                }
                const {data, code} = await balanceTaskPrizeList(this.listQuery)
                if (code === 200) {
                    this.tableData = data.list
                    this.total = data.meta.total
                }
                this.loading = false
        
            },
            async getBalanceTaskList() {
                const {data, code} = await balanceTaskList({
                    page: 1,
                    perPage: 9000
                })
                if (code === 200) {
                    this.options1 = data.list
                }
            },
            async getPetPrizeList() {
                const {data, code} = await petPrizeList({
                    page: 1,
                    perPage: 9000
                })
                if (code === 200) {
                    this.options2 = data.list
                }
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
                this.formData = {
                    id: undefined,
                    prize_id: undefined,
                    count: undefined,
                }
            },
            handleEdit(row) {
                this.dialogVisible = true
                this.formData = JSON.parse(JSON.stringify(row))
            },
            handleAdd() {
                this.dialogVisible = true
            },
            handleDel(row) {
                this.$confirm('确定删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    balanceTaskPrizeDelete({
                        id: row.id,
                    }).then(res => {
                        if(res.code === 200) {
                            this.dialogVisible = false
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            })
                        }
                        this.getTableData()
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消操作'
                    });
                });
            },
            handleAllocationSkill() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        for (let key in this.formData) {
                            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
                        }
                        balanceTaskPrizeSave(this.formData).then(res => {
                            if(res.code === 200) {
                                this.dialogVisible = false
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                                this.dialogVisible = false
                                this.getTableData()
                            }
                        })
                    }
                })
            },
            handleChange(checked) {
                if(checked) {
                    this.formData.max_count = -1
                }else {
                    this.formData.max_count = undefined
                }
            },
            remoteMethod(query) {
                console.log(this.formData.reward_type,query)
                this.query = query
                if (query !== '') {
                    if(this.formData.reward_type === 'gift') {
                        this.handleGetGiftData()
                    }
                    if(this.formData.reward_type === 'pet-prop') {
                        this.getPetPropList()
                    }
                    if(this.formData.reward_type === 'product') {
                        this.getProductList()
                    }
                }
            }
        }
    }
</script>
  
<style lang="scss" scoped>
::v-deep{ 
    .hide_box{
        .el-upload--picture-card {
            display: none;
        }
    }
    .el-upload-list__item {
        margin-right: 1em !important;
    }
}

.image {
    width: 200px;
}
::v-deep {
	.formItemBox{
		.el-form-item {
			width: 50% !important;
		}
	}
    
}

</style>
  